//
//  TakeVideoPreViewController.m
//  NvSellerShow
//
//  Created by bmd on 2017/3/31.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import "TakeVideoPreViewController.h"
#import "NvWindowPlayer.h"
#import "Utils.h"

@interface TakeVideoPreViewController ()<NvWindowPlayerDelegate>

@property (nonatomic, strong) NvWindowPlayer *player;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *okButton;
@property (nonatomic, strong) UILabel *timelabel;

@end

@implementation TakeVideoPreViewController

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:53.0/255 green:63.0/255 blue:77.0/255 alpha:1];
    self.player.frame = CGRectMake(-(SCREEN_HEIGHT-SCREEN_WIDTH)/2, (SCREEN_HEIGHT-SCREEN_WIDTH)/2, SCREEN_HEIGHT, SCREEN_WIDTH);
    self.player.delegate = self;
    self.player.center = self.view.center;
    [self.view addSubview:self.player];
    [self.player setModel:self.model];
    

    [self.player addSubview:self.cancelButton];
    [self.player addSubview:self.okButton];
    [self.player addSubview:self.timelabel];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.width.height.equalTo(@55);
        make.centerY.equalTo(self.player.mas_centerY);
    }];
    [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-20);
        make.width.height.equalTo(@55);
        make.centerY.equalTo(self.player.mas_centerY);
    }];
    
    self.timelabel.text = [NSString stringWithFormat:@"00:00/%@",self.totalTime];
    [self.timelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@15);
        make.centerX.equalTo(self.player.mas_centerX);
        make.width.equalTo(@120);
        make.height.equalTo(@15);
    }];
    [self performSelector:@selector(playTransform) withObject:nil afterDelay:0.3];
}
//MARK:旋转播放器
- (void)playTransform {
    CGAffineTransform  transform = CGAffineTransformRotate(self.player.transform,M_PI/2.0);
    [UIView animateWithDuration:0.3 animations:^{
        self.player.transform = transform;
        [self.view layoutIfNeeded];
    }];
    
}

//MARK:playerDelegate
- (void)nvWindowPlayer:(NvWindowPlayer *)player currentTimeString:(NSString *)currentTimeString totalTimeString:(NSString *)totalTimeString {
    self.timelabel.text = [NSString stringWithFormat:@"%@/%@",currentTimeString,self.totalTime];
}

//MARK:buttonClick
- (void)cancelClick {
    
    [self dismissViewControllerAnimated:YES completion:^{
        if ([self.delegate respondsToSelector:@selector(takeVideoPreViewController:cancelClick:)]) {
            [self.delegate takeVideoPreViewController:self cancelClick:self.cancelButton];
        }
    }];
}

- (void)okClick {
    [self dismissViewControllerAnimated:YES completion:^{
        if ([self.delegate respondsToSelector:@selector(takeVideoPreViewController:okClick:)]) {
            [self.delegate takeVideoPreViewController:self okClick:self.okButton];
        }
    }];
}

//MARK:Getter

- (NvWindowPlayer *)player {
    if (!_player) {
        _player = [NvWindowPlayer instancePlayer];
    }
    return _player;
}
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"NvSellerResource" ofType:@"bundle"];
        NSBundle *myBundle = [NSBundle bundleWithPath:path];
        NSString *imagePath = [myBundle pathForResource:@"cam_cancel" ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setImage:image forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UIButton *)okButton {
    if (!_okButton) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"NvSellerResource" ofType:@"bundle"];
        NSBundle *myBundle = [NSBundle bundleWithPath:path];
        NSString *imagePath = [myBundle pathForResource:@"cam_sure" ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        _okButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_okButton setImage:image forState:UIControlStateNormal];
        [_okButton addTarget:self action:@selector(okClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okButton;
}

- (UILabel *)timelabel {
    if (!_timelabel) {
        _timelabel = [[UILabel alloc] init];
        _timelabel.textAlignment = NSTextAlignmentCenter;
        _timelabel.font = [UIFont systemFontOfSize:20.0f];
        _timelabel.text = @"00:00/00:00";
        _timelabel.textColor = [UIColor whiteColor];
    }
    return _timelabel;
}

//MARK:dealloc
-(void)dealloc {
    [self.player clear];
    NSLog(@"预览视频页面释放");
}

@end
